/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor'];
  return valid_map.indexOf(str.trim()) >= 0;
}

// 金钱、价格,首位不为0,最多2位小数
export function checkNumPot(rule, value, callback) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  if (!value) {
    return callback(new Error('fill in Numbers'));
  } else if (!reg.test(value)) {
    return callback(new Error('up to 2 decimal'));
  } else if (value * 100 > 1000000) {
    return callback(new Error('too large'));
  } else {
    callback();
  }
}

// Sku校验，不能包含全角字符
export function checkHalf(rule, value, callback) {
  if (!value) {
    return callback(new Error('cannot be empty'));
  } else if (value.match(/[^\x00-\xff]/gi)) {
    return callback(new Error('cannot contain full - Angle characters'));
  } else if (value.length > 50) {
    return callback(new Error('the input character is too long'));
  } else {
    callback();
  }
}

// 整数
export function checkInterNum(rule, value, callback) {
  const reg = /^[0-9]*[1-9][0-9]*$/;
  if (!value) {
    return callback(new Error('fill in Numbers'));
  } else if (!reg.test(value)) {
    return callback(new Error('enter an Number'));
  } else if (value * 100 > 1000000) {
    return callback(new Error('too large'));
  } else {
    callback();
  }
}

// 商品价钱

export function checkSalePrice(rule, value, callback, str, that) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  if (!value) {
    return callback(new Error('fill in Numbers'));
  } else if (!reg.test(value)) {
    return callback(new Error('up to 2 decimal places'));
  } else if (str && Number(value) > Number(str)) {
    that.formData.salePrice = null;
    return callback(new Error('less then regular price'));
  } else {
    callback();
  }
}
export function checkRegularPrice(rule, value, callback, str, that) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
  if (!value) {
    return callback(new Error('fill in Numbers'));
  } else if (!reg.test(value)) {
    return callback(new Error('up to 2 decimal places'));
  } else if (str && Number(str) > Number(value)) {
    that.formData.regularPrice = null;
    return callback(new Error('more then sale price'));
  } else {
    callback();
  }
}

// form字符串提交验证
export function validateField(rule, value, callback, max = 50, min = 1) {
  if (!value) {
    callback(new Error('cannot be empty'));
  } else {
    if (value.length < min || value.length > max) {
      callback(new Error(`please enter ${min} to ${max} characters`));
    } else {
      callback();
    }
  }
}

// form下拉框/单选按钮/复选框/日期  提交验证
export function validateSelect(type) {
  if (type === 'date') { // 日期
    return {
      type: 'string',
      required: true,
      message: 'Please select a date',
      trigger: 'change'
    };
  } else if (type === 'array') { // 复选框
    return {
      type: 'array',
      required: true,
      message: 'Please select at least one',
      trigger: 'change'
    };
  } else { // 单选/下拉框
    return { required: true, message: 'Please select an option', trigger: 'change' };
  }
}
